<?php

/**
 * Goods_order_model short summary.
 *
 * Goods_order_model description.
 *
 * @version 1.0
 * @author GALAXY
 */
class Goods_order_model extends CI_Model
{
    public $goods_order_id;
    public $goods_id;
    public $count;
    public $color_id;
    public $size_id;
    public $customer_id;
    public $order_code;
    public $addtime;
    public $order_msg;
    public $order_state;
    public $from_cart_id;
    public $customer_region_id;
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    public function add_order($goodsorder_model)
    {

        $this->db->insert('goods_order',$goodsorder_model);
        $roder_id=$this->db->insert_id();
        if($roder_id>0)
        {
            $sql="SELECT goods_order.*,size.title as sizetitle,color.title as colortitle,goods.title as goodstitle,goods.shop_price,goods.thumbnail_img, goods_style.title as styletitle,customer_region.c_name,customer_region.city,customer_region.county,customer_region.detailed_add,customer_region.phone,customer_region.province,customer_region.tel FROM goods_order LEFT JOIN size on size.size_id=goods_order.size_id LEFT JOIN color on color.color_id=goods_order.color_id LEFT JOIN goods on goods.goods_id=goods_order.goods_id LEFT JOIN goods_style on goods_style.goods_style_id=goods.goods_style_id LEFT JOIN customer_region on customer_region.customer_region_id=goods_order.customer_region_id  where goods_order.goods_order_id=$roder_id";
            return $this->db->query($sql)->row();
        }
        return null;
    }

    //public function goods_add_order

    public function get_order_by_id($goods_order_id)
    {
        $sql="select * from goods_order where goods_order_id=".$goods_order_id;
        return $this->db->query($sql)->row();
    }

    public function get_order_by_customer_cart($customerid,$cartid)
    {
        $sql="SELECT goods_order.*,size.title as sizetitle,color.title as colortitle,goods.title as goodstitle,goods.shop_price,goods.thumbnail_img,goods_style.title as styletitle,customer_region.c_name,customer_region.city,customer_region.county,customer_region.detailed_add,customer_region.phone,customer_region.province,customer_region.tel FROM goods_order LEFT JOIN size on size.size_id=goods_order.size_id LEFT JOIN color on color.color_id=goods_order.color_id LEFT JOIN goods on goods.goods_id=goods_order.goods_id LEFT JOIN goods_style on goods_style.goods_style_id=goods.goods_style_id LEFT JOIN customer_region on customer_region.is_del=0 and  customer_region.customer_region_id=goods_order.customer_region_id  where goods_order.customer_id=$customerid and goods_order.from_cart_id=$cartid";
        //p($sql);
        return $this->db->query($sql)->row();
    }

    public function get_order_by_order_id($order_id)
    {
        $sql="SELECT goods_order.*,size.title as sizetitle,color.title as colortitle,goods.title as goodstitle,goods.shop_price,goods.thumbnail_img,goods_style.title as styletitle,customer_region.c_name,customer_region.city,customer_region.county,customer_region.detailed_add,customer_region.phone,customer_region.province,customer_region.tel FROM goods_order LEFT JOIN size on size.size_id=goods_order.size_id LEFT JOIN color on color.color_id=goods_order.color_id LEFT JOIN goods on goods.goods_id=goods_order.goods_id LEFT JOIN goods_style on goods_style.goods_style_id=goods.goods_style_id LEFT JOIN customer_region on customer_region.is_del=0 and customer_region.customer_region_id=goods_order.customer_region_id  where goods_order.goods_order_id=$order_id ";
        //p($sql);
        return $this->db->query($sql)->row();
    }

    public function update_order_count($orderid,$new_val)
    {
        $sql="update goods_order set count=$new_val where goods_order_id=$orderid";
        return $this->db->query($sql);
    }

    public function set_mail_add($orderid,$region_id)
    {
        $sql="update goods_order set customer_region_id=$region_id where goods_order_id=$orderid";
        return $this->db->query($sql);
    }

    public function get_mail_add($region_id)
    {
        $sql="select * from customer_region where is_del=0 and customer_region_id=$region_id";
        return $this->db->query($sql)->row();
    }

    public function get_to_pay_roder($orderid)
    {
        $sql="SELECT goods_order.*,goods.title as goodstitle,goods.shop_price FROM goods_order LEFT JOIN goods on goods.goods_id=goods_order.goods_id where goods_order_id=$orderid";
        return $this->db->query($sql)->row();
    }

    public function update_order_msg($goods_order_id,$order_msg)
    {
        $sql="update goods_order set order_msg='$order_msg' where goods_order_id=$goods_order_id";
        return $this->db->query($sql);
    }

    public function update_order_state_goods_inventory($order_code,$ali_code)
    {
        $this->db->trans_begin();
        //更新订单状态
        $sql="update goods_order set order_state=1 ,ali_code=$ali_code where order_code='$order_code'";
        $this->db->query($sql);
        //更新库存
        $sql_update_inventory=" UPDATE goods_inventory a INNER JOIN (SELECT goods_inventory.goods_inventory_id,goods_inventory.inventory_count FROM goods_inventory LEFT JOIN goods_order on goods_order.order_code='$order_code' WHERE  goods_order.color_id=goods_inventory.goods_color_id AND goods_order.size_id=goods_inventory.goods_size_id AND goods_order.goods_id=goods_inventory.goods_id) b ON b.goods_inventory_id=a.goods_inventory_id  SET a.inventory_count=b.inventory_count-1";
        $this->db->query($sql_update_inventory);
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return 0;
        }
        else
        {
            $this->db->trans_commit();
            return 1;
        }
    }

    public function get_order_by_customer_id($customer_id)
    {

        $sql="SELECT goods_order.*,goods.title as goodstitle,goods.shop_price,goods.thumbnail_img,size.title as sizetitle,color.title as colortitle ,customer_region.c_name,customer_region.phone,customer_region.tel,customer_region.province,customer_region.city,customer_region.county,customer_region.detailed_add,goods_img.thumbnail_img as goodscolorimg
                FROM goods_order
                LEFT JOIN goods on goods.goods_id=goods_order.goods_id
                LEFT JOIN size on size.size_id=goods_order.size_id
                LEFT JOIN color on color.color_id=goods_order.color_id
                LEFT JOIN customer_region ON customer_region.customer_region_id=goods_order.customer_region_id
                LEFT JOIN goods_img on goods_img.goods_id=goods_order.goods_id and goods_img.goods_color_id=goods_order.color_id
                WHERE goods_order.customer_id=$customer_id order by goods_order.addtime desc";
        return $this->db->query($sql)->result();
    }

    //删除订单
    public function del_order($orderid)
    {
       return  $this->db->delete('goods_order',array('goods_order_id'=>$orderid));
    }

    public function get_exis_order($orderModel)
    {
        $sql="SELECT goods_order.*,size.title as sizetitle,color.title as colortitle,goods.title as goodstitle,goods.shop_price,goods.thumbnail_img,goods_style.title as styletitle,customer_region.c_name,customer_region.city,customer_region.county,customer_region.detailed_add,customer_region.phone,customer_region.province,customer_region.tel FROM goods_order LEFT JOIN size on size.size_id=goods_order.size_id LEFT JOIN color on color.color_id=goods_order.color_id LEFT JOIN goods on goods.goods_id=goods_order.goods_id LEFT JOIN goods_style on goods_style.goods_style_id=goods.goods_style_id LEFT JOIN customer_region on customer_region.is_del=0 and customer_region.customer_region_id=goods_order.customer_region_id where
              goods_order.goods_id=".$orderModel->goods_id." and goods_order.color_id=".$orderModel->color_id." and goods_order.size_id=".$orderModel->size_id." and goods_order.customer_id=".$orderModel->customer_id." and goods_order.order_state=0 and goods_order.deliver_goods_state=0";
        return $this->db->query($sql)->row();
    }

    //查询订单总量
    public function get_count_order()
    {
        $sql="select count(goods_order_id) as order_count from goods_order";
        return $this->db->query($sql)->row();
    }
    //查询未支付订单
    public function get_count_nopay_order()
    {
        $sql="select count(goods_order_id) as order_count from goods_order where order_state=0 and deliver_goods_state=0";
        return $this->db->query($sql)->row();
    }
    //查询已支付订单
    public function get_count_pay_order()
    {
        $sql="select count(goods_order_id) as order_count from goods_order where order_state=1 and deliver_goods_state=0";
        return $this->db->query($sql)->row();
    }
    //查询处理订单
    //查询未处理订单
}